package com.tocchisu.tv.services;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import com.tocchisu.tv.dao.BrandDao;
import com.tocchisu.tv.dao.ChannelDao;
import com.tocchisu.tv.dao.DailyScheduleDAO;
import com.tocchisu.tv.dao.ProgramDao;
import com.tocchisu.tv.model.Advertising;
import com.tocchisu.tv.model.ApplicationParams;
import com.tocchisu.tv.model.Channel;
import com.tocchisu.tv.model.Constants;
import com.tocchisu.tv.model.DailySchedule;
import com.tocchisu.tv.model.DummyProgram;
import com.tocchisu.tv.model.Hourly;
import com.tocchisu.tv.model.Program;
import com.tocchisu.tv.model.ProgrammationException;
import com.tocchisu.tv.model.Slot;

@Service(value = "tvService")
public class TVServiceImpl implements TVService {
  @Autowired
  private ChannelDao channelDAO;
  @Autowired
  private DailyScheduleDAO dailyScheduleDAO;
  @Autowired
  private ProgramDao programDAO;
  @Autowired
  private BrandDao brandDAO;

  private ClassPathXmlApplicationContext context;
  @Autowired
  private ApplicationParams applicationParams;

  public Integer updateAudiences(Long idCompany) {
    Channel channel = channelDAO.find(idCompany);
    Slot lastSlotUpdated = findSlotByDate(channel, channel.getLastAudienceUpdate());
    if (lastSlotUpdated != null) {
      Set<DailySchedule> dailySchedule = channel.getSchedule();
    }
    Date today = new Date();
    return null;
  }

  private Slot findSlotByDate(Channel channel, Date date) {
    return null;
  }

  private Date[] getAds(Date beginDate, Date endDate) {
    return null;
  }

  public Advertising createAd(String name, Long idBrand, Long idContract) {
    Advertising advertising = new Advertising();
    advertising.setBrand(null);
    advertising.setContract(null);
    advertising.setDuration(new Period(0, 5, 0, 0));
    return advertising;
  }

  public Channel createChannel() {
    Channel channel = new Channel();
    channel.setReputation(Constants.INITIAL_REPUTATION);
    Set<DailySchedule> schedule = new TreeSet<DailySchedule>();
    channel.setSchedule(schedule);
    for (int i = 0; i < 5; i++) {
      DailySchedule dailySchedule = new DailySchedule();
      Date dailyScheduleDate = new DateTime().plus(new Period(i, 0, 0, 0)).toDate();
      dailySchedule.setDate(dailyScheduleDate);
      List<Slot> slots = new ArrayList<Slot>();
      for (Hourly hourlyInstance : Hourly.values()) {
        Slot slot = new Slot();
        slot.setHourly(hourlyInstance);
        slot.setDailySchedule(dailySchedule);
        slot.setProgram(new DummyProgram());
        slots.add(slot);
      }
      dailySchedule.setSlots(slots);
      schedule.add(dailySchedule);
    }
    channel.setBroadcast(applicationParams.getInitialBroadcast());
    return channel;
  }

  /**
   * <a href="http://www.lemonde.fr/rss/une.xml">http://www.lemonde.fr/rss/une.
   * xml</a> <a href="http://www.google.fr">http://www.google.fr</a>
   */
  public void getNews() {
    URL feedSource;
    try {
      feedSource = new URL("http://www.lemonde.fr/rss/une.xml");
      SyndFeedInput input = new SyndFeedInput();
      SyndFeed feed = input.build(new XmlReader(feedSource));
      System.out.println(feed.getTitle());
    } catch (MalformedURLException e1) {
    } catch (FeedException e) {

    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  public void addSlot(Long idCompany, Date startingDate, Program program) throws ProgrammationException {
    // TODO Auto-generated method stub

  }

}
